<template>
  <div class="container">
    <div class="login-view" v-if="showVerifyCode">
      <a href="javascript:;" @click="showVerifyCode = false">返回</a>
      <h4 style="text-align: center; color: red">设备号变更需要验证手机号</h4>
      <h5 v-if="userInfo && userInfo.userSecurityInfo" align="center">{{userInfo.userSecurityInfo.countryCode}} {{userInfo.userSecurityInfo.phoneNum}}</h5>
      <div class="form-view">
        <div class="form-item">
          <div class="form-item-label">验证码</div>
          <div class="form-item-content">
            <input type="text" placeholder="请输入验证码" v-model="code">
          </div>
          <div class="form-item-btns">
            <button class="send-code-btn" @click="sendCode">获取验证码</button>
          </div>
        </div>
      </div>
      <button class="submit-btn" @click="submitVerifyCode">提交</button>
    </div>
    <div class="login-view" v-else>
      <div class="tabs">
        <div class="item" :class="{active: loginType == 1}" @click="loginType = 1">手机登录</div>
        <div class="item" :class="{active: loginType == 2}" @click="loginType = 2">账号登录</div>
      </div>
      <div class="form-view">
        <div class="form-item" v-if="loginType == 1">
          <div class="form-item-label">归属地</div>
          <div class="form-item-content">
            <input type="text" placeholder="请输入归属地" v-model="form.countryCode">
          </div>
        </div>
        <div class="form-item" v-if="loginType == 1">
          <div class="form-item-label">手机号</div>
          <div class="form-item-content">
            <input type="text" placeholder="请输入手机号" v-model="form.phoneNum">
          </div>
        </div>
        <div class="form-item" v-if="loginType == 2">
          <div class="form-item-label">账号</div>
          <div class="form-item-content">
            <input type="text" placeholder="请输入账号" v-model="form.account">
          </div>
        </div>
        <div class="form-item">
          <div class="form-item-label">密码</div>
          <div class="form-item-content">
            <input type="text" placeholder="请输入密码" v-model="form.password">
          </div>
        </div>
      </div>
      <button class="submit-btn" @click="submit">提交</button>

      <!-- <pre>{{userInfo}}</pre> -->
    </div>
  </div>
</template>

<script>
export default {
  name: 'login-view',
  props: {
    params: Object,
    userInfo: Object,
    init: Function,
    sendCode: Function,
    verifyCode: Function,
  },
  watch: {
    params: {
      handler(val) {
        this.form = JSON.parse(JSON.stringify(val));
      },
      deep: true,
      immediate: true
    },
    form: {
      handler(val) {
        const params = JSON.stringify(this.params);
        const form = JSON.stringify(val);
        if (params !== form) this.$emit('update:params', JSON.parse(JSON.stringify(val)));
      },
      deep: true,
      immediate: true
    },
    userInfo: {
      handler(val) {
        this.showVerifyCode = val?.sessionToken === '0';
      },
      immediate: true
    }
  },
  data() {
    return {
      showVerifyCode: false,
      loginType: 1,
      code: '',
      form: {
        
      }
    }
  },
  methods: {
    submit() {
      if (this.loginType == 1) {
        delete this.form.account;
        this.form.countryCode = '+86';
        if (this.form.phoneNum.trim() == '') return alert('请输入手机号');
      } else {
        delete this.form.countryCode;
        delete this.form.phoneNum;
        if (this.form.account.trim() == '') return alert('请输入账号');
      }
      if (this.form.password.trim() == '') return alert('请输入密码');
      this.form = { ...this.form }; // 强制刷新字段
      this.init().then(() => {
        this.$emit('submit', this.form);
      });
    },
    submitVerifyCode() {
      if (this.code.trim() == '') return alert('请输入验证码');
      this.verifyCode(this.code);
    }
  }
}
</script>

<style scoped>
.login-view {
  width: 600px;
  margin: 20px auto;
  padding: 10px;
  border-radius: 8px;
  box-shadow: 1px 1px 10px 1px rgba(0,0,0,0.2);
}
.login-view .tabs {
  text-align: center;
  overflow: hidden;
}
.login-view .tabs .item {
  margin: 10px;
  display: inline-block;
  cursor: pointer;
}
.login-view .tabs .item.active {
  font-size: 18px;
  font-weight: bold;
}
.login-view .form-view {
  padding: 10px;
  overflow: hidden;
}
.login-view .form-view .form-item {
  margin: 10px 0;
  overflow: hidden;
  display: flex;
  flex-direction: row;
}
.login-view .form-view .form-item .form-item-label {
  width: 100px;
  overflow: hidden;
}
.login-view .form-view .form-item .form-item-content {
  flex: 1;
  overflow: hidden;
}
.login-view .form-view .form-item .form-item-content input {
  width: 100%;
  padding: 5px 10px;
  box-sizing: border-box;
}

.send-code-btn {
  margin-left: 10px;
  padding: 4px 10px;
}

.submit-btn {
  margin-top: 30px;
  padding: 0;
  width: 100%;
  height: 40px;
  line-height: 40px;
  text-align: center;
  font-size: 18px;
  border: 0;
  border-radius: 8px;
  display: block;
  color: #fff;
  background-color: blue;
}

a {
  color: blue;
}
</style>